<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import {ElMessage, ElMessageBox} from "element-plus";
import {router} from "../../router/index.js";

// 数据参数
const searchQuery = ref('');
const loading = ref(false);
const tableData = ref([]);

const pageSize = ref(5)
const currentPage = ref(1)
const total = ref(1)
const handleSizeChange=(newSize)=>{
  pageSize.value = newSize
  fetchStaffList();

};
const handleCurrentChange=(newPage)=> {
  fetchStaffList();

};


const staff = ref({});
// 获取员工列表
const fetchStaffList = async () => {
  loading.value = true;
  try {
    const response = await axios.post(`/carcenter-service-personnel/staff/getStaffDetail/${currentPage.value}/${pageSize.value}`, staff.value);
    tableData.value = response.data.data.data;
    total.value = response.data.data.total;
  } catch (error) {
    console.error('获取员工列表失败:', error);
  } finally {
    loading.value = false;
  }
};


// 页面加载时获取数据
onMounted(() => {
  fetchStaffList();
});

// 查询操作
const handleSearch = () => {
  currentPage.value = 1; // 重置页码
  fetchStaffList();
};
const handleClear = () =>{
  staff.value = {};
  fetchStaffList();
}


// 编辑操作
const handleEdit = (row) => {
  // 跳转到编辑页面或弹出编辑对话框
  router.push(`/framework/staffEdit?id=${row.id}`)
};

// 删除操作
const handleDelete = (row) => {
  // 弹出确认对话框并执行删除操作
  ElMessageBox.confirm(
      `确定要删除员工 ${row.staffName} 吗？`,
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    try {
      axios.delete(`/carcenter-service-personnel/staff/delete/${row.id}`);
      ElMessage.success('删除成功');
      fetchStaffList();
    } catch (error) {
      console.error('删除失败:', error);
      ElMessage.error('删除失败，请稍后重试');
    }
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};

</script>
<template>
  <div class="staff-management">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="staff" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="staff.key" placeholder="请输入员工姓名/账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch"><el-icon><Search /></el-icon>查询</el-button>
        <el-button @click="handleClear"><el-icon><Delete /></el-icon>清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="staffAccount" label="员工账号"></el-table-column>
      <el-table-column prop="staffName" label="员工姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="roleName" label="员工角色"></el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)"><el-icon><Edit /></el-icon>编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)"><el-icon><Delete /></el-icon>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-size-options="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    />
  </div>
</template>

<style scoped>
.staff-management {
  padding: 20px;
}

.demo-form-inline {
  margin-bottom: 20px;
}
</style>